<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        font-size: 30px;
      }
    </style>
  </head>
  <body>
    <div class="box">abcdefgABCD</div>

    <script>
      var boxEle = document.querySelector(".box");
      var str = boxEle.innerHTML.trim();
      var newstr = "";
      for (i = 0; i < str.length; i++) {
        newstr += `<span>${str[i]}</span>`;
      }
      boxEle.innerHTML = newstr;
      var spans = document.querySelectorAll(".box span");
      document.onkeydown = function (e) {
        var e = e || window.e;
        var key = e.key;
        spans.forEach(function (item) {
          if (key == item.innerHTML) {
            item.style.color = "red";
          }
        });
      };
    </script>
  </body>
</html>
